package com.cie2.janji.components;

import org.apache.log4j.Logger;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.services.Session;

import com.cie2.janji.Visit;
import com.cie2.janji.entities.User;
import com.cie2.janji.entities.User.AccessLevel;
import com.cie2.janji.pages.Start;
import com.cie2.janji.services.LayoutInfoNotifier;


//Another option is using something like
//${asset:context:/css/style.css}. Tapestry will always output the
//correct path.

//This should work:
//
//	<img src="${asset:context:images/some_image.gif}"/>

@IncludeStylesheet("context:style.css")
public class Layout {

	static Logger logger =
	       Logger.getLogger(Layout.class.getName());
	
	@Inject
	private RequestGlobals _requestGlobals;
	
	@Inject
	private LayoutInfoNotifier layoutInfo;
	
// @Property
// private String pageName;
// 
// 
// public String[] getPageNames()
// {
//   return new String[] { "Home", "Register", "Articles", "Promise", "Login" };
// }
     

	Object onActionFromLogOut() {
		Session tapSession = _requestGlobals.getRequest().getSession(false);		
		logger.info("Logging Out User ...");
		
		if (tapSession != null) {
			tapSession.invalidate();
			logger.info("Session invalidated");			
		}
		else {
			logger.info("Session is null");
		}
		
		
		return Start.class;
	}
	
	public Boolean isLogin() {
		Session tapSession = _requestGlobals.getRequest().getSession(false);	
		
		if(tapSession == null) {
			System.out.println(" === Tap session is null");
			return false;
		}
		
		Visit visit = (Visit) tapSession.getAttribute("sso:com.cie2.tip.Visit");
		
		if(visit == null) {
			System.out.println(" === visit is null");
			return false;
		}
		
		return true;

	}
	
	public Boolean isAdmin() {
		Session tapSession = _requestGlobals.getRequest().getSession(false);	
		Visit visit = (Visit) tapSession.getAttribute("sso:com.cie2.tip.Visit");
		
		
		if(visit != null ) {
			User user = visit.getUser();

			if(user != null) {
				if(AccessLevel.Admin.equals(user.getCurrentProfile().getAccessLevel()))
						return true;
			}
		}
			
		return false;
	}	
	
	public Boolean isAutoLogin() {
		return layoutInfo.isAutoLoginOn();
	}
}
